package no.kolstad.movies.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 *
 * @author chriswk
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Lender extends BaseModelObject implements Serializable {
    private Long id;
    private Long version;
    private String name;
    private int mobile;
    private List<Movie> rentals = new ArrayList<Movie>();
    
    @Id @GeneratedValue
    public Long getId() {
      return id;
    }
    public void setId(Long id) {
      this.id = id;
    }
    @Version
    public Long getVersion() {
      return version;
    }
    public void setVersion(Long version) {
      this.version = version;
    }
    
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }

    @Column(unique = true, nullable=false)
    public int getMobile() {
      return mobile;
    }
    public void setMobile(int no) {
      this.mobile = no;
    }
    @OneToMany(cascade=CascadeType.ALL)
    public List<Movie> getRentals() {
      return rentals;
    }
    public void setRentals(List<Movie> rentals) {
      this.rentals = rentals;
    }
    
    @Transient
    public void addRental(Movie rental) {
      List<Movie> currentRentals = getRentals();
      if(currentRentals == null) {
        currentRentals = new ArrayList<Movie>();
      }
      currentRentals.add(rental);
      setRentals(currentRentals);
    }
}
